import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:im_app/im/page/view/ListSeparator.dart';
import 'package:im_app/im/util/DateUtil.dart';

import '../../pojo/ImPojo.dart';
import '../../util/ImConstant.dart';

class MonthView extends StatelessWidget {

  final ImSession session;
  final DateTime month;
  final List<DateTime> days;

  const MonthView({ required this.session, required this.month, required this.days, super.key});

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: const EdgeInsets.only(left: 20, top: 10, bottom: 2),
          child: Text(DateUtil.formatDate2(month, "yyyy年MM日")),
        ),
        ListSeparator(
          indent: 0,
          endIndent: 0,
        ),
        Container(
          padding: EdgeInsets.only(left: 30, right: 10),
          constraints: BoxConstraints(maxHeight: 200),
          child: GridView(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 7, childAspectRatio: 1.5),
            children: _buildMonthDayCell(context),
          ),
        )
      ],
    );
  }

  int getLastedMonthDay(DateTime date) {
    return date.month == 1 ? getDaysInMonth(date.year - 1, 12) : getDaysInMonth(date.year, date.month - 1);
  }

  int getDaysInMonth(int year, int month) {
    final DateTime startDate = DateTime(year, month, 1);
    final DateTime endDate = month == 12
        ? DateTime(
            year + 1,
            1,
            1,
          )
        : DateTime(year, month + 1, 1);
    return endDate.difference(startDate).inDays;
  }

  List<Widget> _buildMonthDayCell(BuildContext context) {
    List<Widget> result = [];

    DateTime firstMonthDay = DateTime(month.year, month.month, 1);
    int weekDay = firstMonthDay.weekday;
    for (var i = 1; i < weekDay; i++) {
      result.add(const Text(''));
    }

    int days = getLastedMonthDay(month);
    for (var i = 0; i < days; i++) {
      bool hasData = false;
      for (var day in this.days) {
        if (day.day == (i + 1)) {
          hasData = true;
          break;
        }
      }
      if (hasData) {
        var text = GestureDetector(
          child: Text("${i + 1}", style: TextStyle(fontSize: ImConstant.IM_DEFAULT_SIZE, fontWeight: FontWeight.w500)),
          onTap: () {_showMessage(context, i+1);},
        );
        result.add(text);
      } else {
        var text = Text(
          "${i + 1}",
          style: TextStyle(color: Colors.grey),
        );
        result.add(text);
      }
    }

    DateTime lastedMonthDay = DateTime(month.year, month.month, days);
    weekDay = lastedMonthDay.weekday;
    for (var i = (weekDay + 1); i <= 7; i++) {
      result.add(const Text(''));
    }

    return result;
  }

  void _showMessage(BuildContext context, int day) {
    var currentDay = DateTime(month.year, month.month, day);
    Navigator.of(context).pushNamedAndRemoveUntil("/MessageSendPage", ModalRoute.withName('/MainPage'), arguments: {"id": session?.id!, "name": session?.name, "day":
      currentDay});
  }
}
